from django.db import models
import hashlib


class User(models.Model):
    SEX_CHOICES = [
        ('male', 'Male'),
        ('female', 'Female'),
    ]
    name = models.CharField(max_length=250, verbose_name='Name')
    username = models.CharField(max_length=250, verbose_name='Username')
    password = models.CharField(max_length=250, verbose_name='Password')
    age = models.IntegerField(verbose_name='Age')
    sex = models.CharField(max_length=10, verbose_name='Sex', choices=SEX_CHOICES)
    phone_number = models.CharField(max_length=20, blank=True, verbose_name='Phone Number')
    address = models.CharField(max_length=200, verbose_name='Address')
    id_card = models.CharField(max_length=30, blank=True, verbose_name='ID Card')
    allergy_information = models.TextField(blank=True, verbose_name='Allergy Information')
    is_active = models.BooleanField(verbose_name='Active', default=True)
    superuser = models.BooleanField(default=False)
    is_doctor = models.BooleanField(default=False)

    def __str__(self):
        return self.username

    def check_password(self, password):
        hashed_password = hashlib.sha256(password.encode()).hexdigest()
        user = User.objects.get(username=self.username, password=hashed_password)
        if not user:
            return False
        return True

    def set_password(self, password):
        hashed_password = hashlib.sha256(password.encode()).hexdigest()
        self.password = hashed_password
